/*
 * Copyright (C) 2004-2006  Autodesk, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */


#include "stdafx.h"
#include "FdoWcsUtils.h"
#include "FdoWcsCoverageSummary.h"
#include "FdoOwsWGS84BoundingBox.h"
#include "FdoWcsCoverageSummaryCollection.h"
#include "FdoWcsGlobals.h"

// the default message catalog filename
#ifdef _WIN32
char *fdofdowcs_cat = "WCSMessage.dll";
#else
char *fdofdowcs_cat = "FdoWcsMessage.cat";
#endif

const wchar_t* URNOGCCRS = L"urn:ogc:def:crs:";
const wchar_t* DOUBLECOLON = L"::";

FdoString* NlsMsgGet(int msg_num, char* default_msg, ...)
{
    va_list varargs;
    va_start(varargs, default_msg);
    FdoString* ret = FdoException::NLSGetMessage(msg_num, default_msg, fdofdowcs_cat, varargs);
    va_end(varargs);

    return ret;
}

FdoString* NlsMsgGet(int msg_num, char* default_msg, char* file, int line, ...)
{
    va_list varargs;
    va_start(varargs, line);
    FdoString* ret = FdoException::NLSGetMessage (msg_num, default_msg, file, line, fdofdowcs_cat, varargs);
    va_end(varargs);

    return ret;
}

void _calcCoverageBoundingBox(FdoWcsCoverageSummary* coverage, FdoString* crsName, FdoOwsBoundingBox* bbox)
{
    FdoPtr<FdoOwsBoundingBoxCollection> bboxes = coverage->GetBoundingBoxCollection();
    for (int i =0;i<bboxes->GetCount();i++)
    {
        FdoPtr<FdoOwsBoundingBox> tempBBox = bboxes->GetItem(i);
        if (wcscmp(tempBBox->GetCRS(),crsName) ==0)
        {
            bbox->SetMaxX (tempBBox->GetMaxX ());
            bbox->SetMinX (tempBBox->GetMinX ());
            bbox->SetMaxY (tempBBox->GetMaxY ());
            bbox->SetMinY (tempBBox->GetMinY ());
			
			break;
        }
    }

	// TODO: Try to get the bounding box from coverage description if not found in coverage summary.	
	// TODO: Convert one of found bounding boxes to the one with the specified CRS if not found yet.
}


FdoString* _parseOgcDefCrs(FdoString* ogcDefCrs)
{
	if (ogcDefCrs == NULL)
		return NULL;

	wchar_t* ret = NULL;
    wchar_t* delimStart = wcsstr( (wchar_t*)ogcDefCrs, URNOGCCRS );

	if ( delimStart) {
        wchar_t* delimEnd = delimStart + wcslen(URNOGCCRS);
		int len = (int)wcslen(delimEnd);
		ret = new wchar_t[len + 1];
		ret[len] = L'\0';
		ret = wcscpy(ret, delimEnd);
	}
	
    // if no version substring, remove the one of ":" in "::".
	delimStart = wcsstr(ret, DOUBLECOLON);
	while(delimStart && *delimStart != L'\0')
	{
		*delimStart = *(delimStart+1);
		++delimStart;
	}

	// TODO: remove the version substring, if any.

	return (ret == NULL) ? ogcDefCrs : ret;
}